package com.abss.vote.util;

import com.abss.vote.model.MpInfo;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static java.awt.Image.SCALE_AREA_AVERAGING;

/**
 * 文件相关操作的工具
 *
 */
public class FileUtils {

    private static final String HTMLNAME="article/";
    private static final String ACTIVITYHTMLNAME="activity/";
    /**
     * 保存图片
     *      将需要保存的图片按照指定的像素进行缩小或放大
     *      可选是否保存原有的图片(在使用完毕后建议删除)
     * @param file 图片位置
     * @return 相对路径
     */
    public static String saveImage(File file, String name, MpInfo mp){
        int w=750;
        try {
            BufferedImage bufferedImage = ImageIO.read(file);
            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();
            //长宽比例
            double bl = (double)height/(double)width;
            //获取缩放或扩大后的图片
            Image image = bufferedImage.getScaledInstance(w,(int)(w*bl),SCALE_AREA_AVERAGING);
            //创建空的画板
            BufferedImage outputImage = new BufferedImage(w,(int)(w*bl), BufferedImage.TYPE_INT_RGB);
            Graphics graphics = outputImage.getGraphics();
            graphics.drawImage(image, 0, 0, null);
            graphics.dispose();
            File saveFile = new File(mp.getFILE_PATH()+"/work/"+name);
            //父路径不存在则创建
            if(saveFile.getParentFile().exists()){
                saveFile.exists();
            }
            //保存为临时文件
            if(ImageIO.write(outputImage, "jpg",saveFile)){
                return  mp.getDOMAIN_NAME()+"/work/"+name;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(true){
                //删除临时文件
                if(file != null && file.isFile()){
                    file.delete();
                }
            }
        }
        return null;
    }


    /**
     * 保存LOGO图片
     *      将图片保存到此服务器
     * @param file 图片位置
     * @return 相对路径
     */
    public static String saveLogo(File file,String name,MpInfo mp){
        int w=750;
        try {
            BufferedImage bufferedImage = ImageIO.read(file);
            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();
            //长宽比例
            double bl = (double)height/(double)width;
            //获取缩放或扩大后的图片
            Image image = bufferedImage.getScaledInstance(w,(int)(w*bl),SCALE_AREA_AVERAGING);
            //创建空的画板
            BufferedImage outputImage = new BufferedImage(w,(int)(w*bl), BufferedImage.TYPE_INT_RGB);
            Graphics graphics = outputImage.getGraphics();
            graphics.drawImage(image, 0, 0, null);
            graphics.dispose();
            File saveFile = new File(mp.getFILE_PATH()+"/logo/"+name);
            //父路径不存在则创建
            if(saveFile.getParentFile().exists()){
                saveFile.exists();
            }
            //保存为临时文件
            if(ImageIO.write(outputImage, "jpg",saveFile)){
                return "/logo/"+name;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(true){
                //删除临时文件
                if(file != null && file.isFile()){
                    file.delete();
                }
            }
        }
        return null;
    }


    /**
     * 获取配置文件中的数据
     * APP_ID
     * APP_SECRET
     * SERVER_ID
     */
    private static SAXReader reader = new SAXReader();


    public static List<String> getBanner() throws DocumentException {
        File file = new File("E:/activity/Vote/Remote.xml");//爱不释书
        Document document = reader.read(file);
        Element root = document.getRootElement();
        List<Element> list = root.elements();
        List<String> ls = new ArrayList<String>();
        for(int i=0;i<list.size();i++){
            ls.add(list.get(i).getText());
        }
        return ls;
    }


    /**
     * 保存Html文件到本服务器
     * 获取的是本服务器的保存路径
     * @param text
     * @param middlePath
     * @return
     */
    public static String saveHtml(String text,String middlePath,MpInfo mp){
        String filepath = mp.getFILE_PATH()+HTMLNAME+middlePath;
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath),"UTF-8"));
            bufferedWriter.write(text);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(bufferedWriter != null){
                    bufferedWriter.flush();
                    bufferedWriter.close();

                    return mp.getDOMAIN_NAME()+"/"+HTMLNAME+middlePath;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return "";
    }
    /**
     * 保存Html文件到本服务器
     * 获取的是本服务器的保存路径
     * @param text
     * @param middlePath
     * @return
     */
    public static String saveActivityHtml(String text,String middlePath , MpInfo mp){
        String filepath = mp.getFILE_PATH()+ACTIVITYHTMLNAME+middlePath;
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath),"UTF-8"));
            bufferedWriter.write(text);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(bufferedWriter != null){
                    bufferedWriter.flush();
                    bufferedWriter.close();

                    return mp.getDOMAIN_NAME()+"/"+ACTIVITYHTMLNAME+middlePath;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return "";
    }
}
